package com.ayoka.parenting.ui.group.index;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.CircleGroupBean;
import com.ayoka.parenting.bean.PagerBean;
import com.ayoka.parenting.http.API;
import com.ayoka.parenting.http.HttpBeanInterceptCallback;
import com.ayoka.parenting.interf.CallbackUtil;
import com.ayoka.parenting.interf.RequestCallback;
import com.ayoka.parenting.mvp.BasePresenter;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GroupIndexModel&.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class GroupIndexModel implements GroupIndexContract.Model{


    @Override
    public void getCircleGroupListData(final BasePresenter presenter, final int pageNo, final int pageSize, final RequestCallback<PagerBean<CircleGroupBean>> callback) {
        API.getCircleGroupListData(presenter, pageNo, pageSize, new HttpBeanInterceptCallback<BaseResponse<PagerBean<CircleGroupBean>>>() {
            @Override
            public void onSuccess(BaseResponse<PagerBean<CircleGroupBean>> response) {
                CallbackUtil.notifySuccess(callback, response.data);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    getCircleGroupListData(presenter, pageNo, pageSize, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void joinGroup(final BasePresenter presenter, final String groupId, final RequestCallback<BaseResponse> callback) {
        API.joinGroup(presenter, groupId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    joinGroup(presenter, groupId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }
}
